package org.example.demo_ottery.common.pojo;

import lombok.Data;
import org.example.demo_ottery.common.errorcode.ErrorCode;
import org.example.demo_ottery.common.errorcode.GlobalErrorCodeConstants;
import org.springframework.util.Assert;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String massage;

    /**
     * 成功响应的数据
     */
    private T data;

    public static <T> CommonResult<T> success (T data) {
        CommonResult<T> result=new CommonResult<>();
        result.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        result.setData(data);
        result.setMassage(GlobalErrorCodeConstants.SUCCESS.getMassage());
        return result;
    }

    public static <T> CommonResult<T> error(Integer code,String massage) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),
                "code不是错误的异常");
        CommonResult<T> result=new CommonResult<>();
        result.setCode(code);
        result.setMassage(massage);
        return result;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        CommonResult<T> result=error(errorCode.getCode(),errorCode.getMassage());
        return result;
    }
}
